package com.becca.cake.controller.payment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.core.common.PagingBean;
import com.becca.cake.core.exception.CommonExceptionHandle;
import com.becca.cake.model.payment.InvoiceModel;
import com.becca.cake.model.payment.OrderRefundModel;
import com.becca.cake.model.payment.PaymentModel;
import com.becca.cake.service.payment.IPaymentService;
import com.becca.cake.util.JsonUtil;

/**
 * 财务管理Controller
 * @copyright  尊星(上海)信息科技有限公司
 * @author yellow
 * @date 2016年5月20日 上午10:29:23
 */
@Controller
@RequestMapping("/paymentManager")
public class PaymentAction extends BaseAction {
    private final static Log logger = LogFactory.getLog(PaymentAction.class);

    @Autowired
    private IPaymentService  iPaymentService;

    /**
     * 查询财务数据
     * @param paymentModel
     * @return
     */
    @RequestMapping(value = "/selectPayments", method = RequestMethod.POST)
    @ResponseBody
    public String selectPayments(PaymentModel paymentModel) {
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("paymentModel", paymentModel);
        map.put("pagingBean", page);
        try {
            // stmp1:查询订单数据
            List<PaymentModel> list = this.iPaymentService.selectPayments(map);
            // stmp2:获取订单总数
            Long count = this.iPaymentService.selectPaymentsCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 查询发票列表
     * @param paymentModel
     * @return
     */
    @RequestMapping(value = "/selectInvoices", method = RequestMethod.POST)
    @ResponseBody
    public String selectInvoices(InvoiceModel invoiceModel) {
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("invoiceModel", invoiceModel);
        map.put("pagingBean", page);
        try {
            // stmp1:查询订单数据
            List<InvoiceModel> list = this.iPaymentService.selectInvoices(map);
            // stmp2:获取订单总数
            Long count = this.iPaymentService.selectInvoicesCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            logger.error("查询发票信息失败!", e);
            return JsonUtil.returnJsonUnSuccess("查询发票信息失败!");
        }
    }

    /**
     * 批量修改发票信息为 已开票
     * @param jsonList
     * @return
     */
    @RequestMapping(value = "/updIsReferenceInvoice", method = RequestMethod.POST)
    @ResponseBody
    public String updIsReferenceInvoice(@RequestParam(value = "jsonList", required = false) String jsonList) {
        try {
            this.iPaymentService.updIsReferenceInvoice(jsonList);
            return JsonUtil.returnJsonSuccess("操作发票信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }

    }

    /**
     * 批量导出发票数据
     * @param jsonList
     * @return
     */
    @RequestMapping(value = "/exportInvoicesToExcel", method = RequestMethod.GET)
    public void exportInvoicesToExcel(HttpServletRequest request,
                                      HttpServletResponse response,
                                      @RequestParam(value = "orderIds", required = false) String orderIds) {
        try {
            this.iPaymentService.exportInvoicesToExcel(request, response, orderIds);
        } catch (Exception e) {
            logger.error("批量导出发票数据失败!", e);
        }
    }

    /**
     * 订单退款查询列表
     * @param paymentModel
     * @return
     */
    @RequestMapping(value = "/selectOrderRefunds", method = RequestMethod.POST)
    @ResponseBody
    public String selectOrderRefunds(OrderRefundModel orderRefundModel) {
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orderRefundModel", orderRefundModel);
        map.put("pagingBean", page);
        try {
            // stmp1:查询订单退款数据
            List<OrderRefundModel> list = this.iPaymentService.selectOrderRefunds(map);
            // stmp2:获取订单退款总数
            Long count = this.iPaymentService.selectOrderRefundsCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
}
